﻿using System;
using System.Data;
using System.Text;
using System.Data.SQLite;

namespace Clipper.DAL
{
	/// <summary>
	/// 数据访问类:QCReference
	/// </summary>
	public partial class QCReference
	{
		public QCReference()
		{}
		#region  BasicMethod

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(string DetectItem)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from QCReference");
			strSql.Append(" where DetectItem=@DetectItem ");
			SQLiteParameter[] parameters = {
					new SQLiteParameter("@DetectItem", DbType.String,20)			};
			parameters[0].Value = DetectItem;

			return SQLiteHelper.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public bool Add(Clipper.Model.QCReference model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into QCReference(");
			strSql.Append("DetectItem,chromeTop1,chromeLow1,chromeTop2,chromeLow2,chromeMean1,standardDeviation1,chromeMean2,standardDeviation2)");
			strSql.Append(" values (");
			strSql.Append("@DetectItem,@chromeTop1,@chromeLow1,@chromeTop2,@chromeLow2,@chromeMean1,@standardDeviation1,@chromeMean2,@standardDeviation2)");
			SQLiteParameter[] parameters = {
					new SQLiteParameter("@DetectItem", DbType.String,20),
					new SQLiteParameter("@chromeTop1", DbType.Decimal,8),
					new SQLiteParameter("@chromeLow1", DbType.Decimal,8),
					new SQLiteParameter("@chromeTop2", DbType.Decimal,8),
					new SQLiteParameter("@chromeLow2", DbType.Decimal,8),
					new SQLiteParameter("@chromeMean1", DbType.Decimal,8),
					new SQLiteParameter("@standardDeviation1", DbType.Decimal,8),
					new SQLiteParameter("@chromeMean2", DbType.Decimal,8),
					new SQLiteParameter("@standardDeviation2", DbType.Decimal,8)};
			parameters[0].Value = model.DetectItem;
			parameters[1].Value = model.chromeTop1;
			parameters[2].Value = model.chromeLow1;
			parameters[3].Value = model.chromeTop2;
			parameters[4].Value = model.chromeLow2;
			parameters[5].Value = model.chromeMean1;
			parameters[6].Value = model.standardDeviation1;
			parameters[7].Value = model.chromeMean2;
			parameters[8].Value = model.standardDeviation2;

			int rows=SQLiteHelper.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(Clipper.Model.QCReference model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update QCReference set ");
			strSql.Append("chromeTop1=@chromeTop1,");
			strSql.Append("chromeLow1=@chromeLow1,");
			strSql.Append("chromeTop2=@chromeTop2,");
			strSql.Append("chromeLow2=@chromeLow2,");
			strSql.Append("chromeMean1=@chromeMean1,");
			strSql.Append("standardDeviation1=@standardDeviation1,");
			strSql.Append("chromeMean2=@chromeMean2,");
			strSql.Append("standardDeviation2=@standardDeviation2");
			strSql.Append(" where DetectItem=@DetectItem ");
			SQLiteParameter[] parameters = {
					new SQLiteParameter("@chromeTop1", DbType.Decimal,8),
					new SQLiteParameter("@chromeLow1", DbType.Decimal,8),
					new SQLiteParameter("@chromeTop2", DbType.Decimal,8),
					new SQLiteParameter("@chromeLow2", DbType.Decimal,8),
					new SQLiteParameter("@chromeMean1", DbType.Decimal,8),
					new SQLiteParameter("@standardDeviation1", DbType.Decimal,8),
					new SQLiteParameter("@chromeMean2", DbType.Decimal,8),
					new SQLiteParameter("@standardDeviation2", DbType.Decimal,8),
					new SQLiteParameter("@DetectItem", DbType.String,20)};
			parameters[0].Value = model.chromeTop1;
			parameters[1].Value = model.chromeLow1;
			parameters[2].Value = model.chromeTop2;
			parameters[3].Value = model.chromeLow2;
			parameters[4].Value = model.chromeMean1;
			parameters[5].Value = model.standardDeviation1;
			parameters[6].Value = model.chromeMean2;
			parameters[7].Value = model.standardDeviation2;
			parameters[8].Value = model.DetectItem;

			int rows=SQLiteHelper.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(string DetectItem)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from QCReference ");
			strSql.Append(" where DetectItem=@DetectItem ");
			SQLiteParameter[] parameters = {
					new SQLiteParameter("@DetectItem", DbType.String,20)			};
			parameters[0].Value = DetectItem;

			int rows=SQLiteHelper.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string DetectItemlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from QCReference ");
			strSql.Append(" where DetectItem in ("+DetectItemlist + ")  ");
			int rows=SQLiteHelper.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Clipper.Model.QCReference GetModel(string DetectItem)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select DetectItem,chromeTop1,chromeLow1,chromeTop2,chromeLow2,chromeMean1,standardDeviation1,chromeMean2,standardDeviation2 from QCReference ");
			strSql.Append(" where DetectItem=@DetectItem ");
			SQLiteParameter[] parameters = {
					new SQLiteParameter("@DetectItem", DbType.String,20)			};
			parameters[0].Value = DetectItem;

			Clipper.Model.QCReference model=new Clipper.Model.QCReference();
			DataSet ds=SQLiteHelper.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Clipper.Model.QCReference DataRowToModel(DataRow row)
		{
			Clipper.Model.QCReference model=new Clipper.Model.QCReference();
			if (row != null)
			{
				if(row["DetectItem"]!=null)
				{
					model.DetectItem=row["DetectItem"].ToString();
				}
				if(row["chromeTop1"]!=null && row["chromeTop1"].ToString()!="")
				{
					model.chromeTop1=decimal.Parse(row["chromeTop1"].ToString());
				}
				if(row["chromeLow1"]!=null && row["chromeLow1"].ToString()!="")
				{
					model.chromeLow1=decimal.Parse(row["chromeLow1"].ToString());
				}
				if(row["chromeTop2"]!=null && row["chromeTop2"].ToString()!="")
				{
					model.chromeTop2=decimal.Parse(row["chromeTop2"].ToString());
				}
				if(row["chromeLow2"]!=null && row["chromeLow2"].ToString()!="")
				{
					model.chromeLow2=decimal.Parse(row["chromeLow2"].ToString());
				}
				if(row["chromeMean1"]!=null && row["chromeMean1"].ToString()!="")
				{
					model.chromeMean1=decimal.Parse(row["chromeMean1"].ToString());
				}
				if(row["standardDeviation1"]!=null && row["standardDeviation1"].ToString()!="")
				{
					model.standardDeviation1=decimal.Parse(row["standardDeviation1"].ToString());
				}
				if(row["chromeMean2"]!=null && row["chromeMean2"].ToString()!="")
				{
					model.chromeMean2=decimal.Parse(row["chromeMean2"].ToString());
				}
				if(row["standardDeviation2"]!=null && row["standardDeviation2"].ToString()!="")
				{
					model.standardDeviation2=decimal.Parse(row["standardDeviation2"].ToString());
				}
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select DetectItem,chromeTop1,chromeLow1,chromeTop2,chromeLow2,chromeMean1,standardDeviation1,chromeMean2,standardDeviation2 ");
			strSql.Append(" FROM QCReference ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return SQLiteHelper.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM QCReference ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = SQLiteHelper.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.DetectItem desc");
			}
			strSql.Append(")AS Row, T.*  from QCReference T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return SQLiteHelper.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SQLiteParameter[] parameters = {
					new SQLiteParameter("@tblName", DbType.VarChar, 255),
					new SQLiteParameter("@fldName", DbType.VarChar, 255),
					new SQLiteParameter("@PageSize", DbType.Int32),
					new SQLiteParameter("@PageIndex", DbType.Int32),
					new SQLiteParameter("@IsReCount", DbType.bit),
					new SQLiteParameter("@OrderType", DbType.bit),
					new SQLiteParameter("@strWhere", DbType.VarChar,1000),
					};
			parameters[0].Value = "QCReference";
			parameters[1].Value = "DetectItem";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return SQLiteHelper.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  BasicMethod
		#region  ExtensionMethod

		#endregion  ExtensionMethod
	}
}

